package com.yanqu.road.dao.impl.activity.langyabang;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyabang.data.LangYaDeclareWarNoticeData;
import com.yanqu.road.entity.activity.langyabang.data.LangYaUnionBriefData;
import com.yanqu.road.entity.activity.langyabang.data.LangYaUserBriefData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LangYaBangDeclareNoticeDaoImpl extends TempDataObjectDao<LangYaDeclareWarNoticeData> {
    public static String getLangYaDeclareWarNoticeDataSql = "select * from `t_c_lang_ya_declare_notice` where `activityId`=? and `groupId`=? order by time desc limit ?";
    public List<LangYaDeclareWarNoticeData> getLangYaDeclareWarNoticeData(int activityId, int groupId,int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<LangYaDeclareWarNoticeData> dataList= new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getLangYaDeclareWarNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaDeclareWarNoticeData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateLangYaDeclareWarNoticeDataSql = "update `t_c_lang_ya_declare_notice` set declareWarUid=?,attackUser=?,attackUnion=?,defendUnion=?,time=?";
    @Override
    protected boolean updateData(LangYaDeclareWarNoticeData langYaDeclareWarNoticeData) {
        langYaDeclareWarNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, langYaDeclareWarNoticeData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaDeclareWarNoticeData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaDeclareWarNoticeData.getDeclareWarUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaDeclareWarNoticeData.getAttackUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaDeclareWarNoticeData.getAttackUnion())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaDeclareWarNoticeData.getDefendUnion())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaDeclareWarNoticeData.getTime()));
        if (getDb().executeNoneQuery(updateLangYaDeclareWarNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertLangYaDeclareWarNoticeDataSql = "insert into `t_c_lang_ya_declare_notice`(activityId,groupId,declareWarUid,attackUser,attackUnion,defendUnion,time) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(LangYaDeclareWarNoticeData langYaDeclareWarNoticeData) {
        langYaDeclareWarNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, langYaDeclareWarNoticeData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaDeclareWarNoticeData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaDeclareWarNoticeData.getDeclareWarUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaDeclareWarNoticeData.getAttackUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaDeclareWarNoticeData.getAttackUnion())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaDeclareWarNoticeData.getDefendUnion())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaDeclareWarNoticeData.getTime()));
        if (getDb().executeNoneQuery(insertLangYaDeclareWarNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected LangYaDeclareWarNoticeData parseData(ResultSet rs) throws SQLException {
        LangYaDeclareWarNoticeData langYaDeclareWarNoticeData = new LangYaDeclareWarNoticeData();
        langYaDeclareWarNoticeData.setActivityId(rs.getInt("activityId"));
        langYaDeclareWarNoticeData.setGroupId(rs.getInt("groupId"));
        langYaDeclareWarNoticeData.setDeclareWarUid(rs.getInt("declareWarUid"));
        langYaDeclareWarNoticeData.setAttackUser(JSON.parseObject(rs.getString("attackUser"), LangYaUserBriefData.class));
        langYaDeclareWarNoticeData.setAttackUnion(JSON.parseObject(rs.getString("attackUnion"), LangYaUnionBriefData.class));
        langYaDeclareWarNoticeData.setDefendUnion(JSON.parseObject(rs.getString("defendUnion"), LangYaUnionBriefData.class));
        langYaDeclareWarNoticeData.setTime(rs.getLong("time"));

        return langYaDeclareWarNoticeData;
    }


}
